﻿using System;

namespace FlashPay.Admin.Controllers
{
    using FlashPay.Entity;
    using FlashPay.Entity.Enum;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.Transfer;
    using FlashPay.Entity.Response.User;
    using FlashPay.Service.Interface;
    using FlashPay.Util;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;

    /// <summary>
    /// 中转卡控制器
    /// </summary>
    /// <remarks>2018-10-01 immi 创建</remarks>
    public class TransferController : BaseController
    {
        #region 注入
        /// <summary>
        /// 用户业务接口
        /// </summary>
        private readonly BankService _bankService;

        /// <summary>
        /// 中转卡业务接口
        /// </summary>
        private readonly TransferService _transferService;


        /// <summary>
        /// 余额变化业层接口
        /// </summary>
        private readonly AdjustBalanceService _adjustBalanceService;
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_manage"></param>
        public TransferController(IAuthenticate<TicketResponse> _manage, IHttpContextAccessor _accessor, BankService bankService, TransferService transferService, AdjustBalanceService adjustBalanceService) : base(_manage, _accessor)
        {
            _bankService = bankService;
            _transferService = transferService;
            _adjustBalanceService = adjustBalanceService;
        }
        #endregion

        #region 中转卡管理
        /// <summary>
        /// 获取中转卡
        /// </summary>
        /// <param name="bcId">银行卡编号</param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.TransferCard0003)]
        public JsonResult GetTransferCard(int bcId)
        {
            var response = _bankService.Get(_manage.data.CompanyID, bcId, _manage.data.UserPermission);

            return Json(response);
        }

        /// <summary>
        /// 中转卡管理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.TransferCard0001)]
        public JsonResult TransferCard(TransferCardQuery query)
        {
            query.CompanyId = _manage.data.CompanyID;

            var response = _transferService.TransferCardPager(query);

            return Json(response);
        }

        /// <summary>
        /// 中转卡更新状态
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.TransferCard0002)]
        public JsonResult TransferCardUpdateStatus(int id, SByte status)
        {
            //输出
            var response = new JResult()
            {
                Success = false
            };

            //银行卡状态
            var enableStatus = new List<sbyte>()
            {
                (sbyte)EnableStatus.有效.GetHashCode(),
                (sbyte)EnableStatus.禁用.GetHashCode()
            };

            //验证参数是否包含在数组中
            if (enableStatus.Contains(status))
            {
                response = _bankService.UpdateEnableStatus(id, status, _manage.data.UserName);
            }
            else
            {
                response.ErrorMessage = "参数错误！";
            }


            return Json(response);
        }
        #endregion

        #region 中转卡记录
        /// <summary>
        /// 中转卡记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.TransferCardRecord0001)]
        public JsonResult TransferCardRecord(TransferCardRecordQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {

                    var startTime = Convert.ToDateTime(query.StartTime);
                    var endTime = Convert.ToDateTime(query.EndTime);

                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }
            query.CompanyId = _manage.data.CompanyID;

            var response = _transferService.TransferCardRecordPager(query);

            return Json(response);
        }

        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <returns>JResult</returns>
        [AuthorizeFilter(AuthCode.TransferCardRecord0002)]
        public JsonResult Reset(long orderNo)
        {
            var reqeust = new ResetReqeust()
            {
                OrderNo = orderNo,
            };

            var response = _transferService.Reset(reqeust);

            return Json(response);
        }

        /// <summary>
        /// 转失败
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns>JResult</returns>
        [AuthorizeFilter(AuthCode.TransferCardRecord0003)]
        public JsonResult TurnFailed(long orderNo)
        {
            var request = new TurnFailedReqeust(){
                OrderNo = orderNo
            };

            var response = _transferService.TurnFailed(request);

            return Json(response);
        }

        /// <summary>
        /// 转成功
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns>JResult</returns>
        [AuthorizeFilter(AuthCode.TransferCardRecord0005)]
        public JsonResult TurnSuccess(long orderNo)
        {
            var request = new TurnSuccessReqeust()
            {
                OrderNo = orderNo
            };

            var response = _transferService.TurnSuccess(request);

            return Json(response);
        }

        /// <summary>
        /// 重新付款
        /// </summary>
        /// <param name="request"></param>
        [AuthorizeFilter(AuthCode.TransferCardRecord0004)]
        public JsonResult Repayment(RepaymentRequest request)
        {
            var response = _transferService.Repayment(request);

            return Json(response);
        }

        /// <summary>
        /// 验证重新付款
        /// </summary>
        /// <param name="request"></param>
        [AuthorizeFilter(AuthCode.TransferCardRecord0001)]
        public JsonResult VerifyRepayment(long orderNo)
        {
            var response = _transferService.VerifyRepayment(orderNo);

            return Json(response);
        }
        #endregion
    }
}